﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Transactions;
using System.Web.Http;
using SalesSys.BLL;
using SalesSys.DAL.Models;
using SalesSys.Site.Models;
using SalesSys.WebCore.Authorize;
using SalesSys.Comm.BLL;
using System.IO;
using hlib.IO;
using SalesSys.Comm.Utility;
using SalesSys.Service.Sales;
using SalesSys.BLL.Sales;

namespace SalesSys.Site.Controllers.Api
{
    /// <summary>
    /// 订单运营管理
    /// </summary>
    [RoutePrefix("api/OrderManager")]
    public class OrderManagerController : BaseApiController
    {
        OrderBLL orderBll = BllFactory.GetBll<OrderBLL>();
        private IDirectSale directSaleSvc = new SalesSvc();
      

        #region OrderEdit

        /// <summary>
        /// 基本信息修改，不允许修改：客户、仓库、省市，户头
        /// </summary>
        /// <param name="orderDto">计划单模型类</param>
        /// <returns></returns>
        [Route("OrderBaseEdit")]
        public bool OrderBaseEdit(OrderDto orderDto)
        {
            var old = orderBll.Details(orderDto.Id);
            var msg = "";
            if (old.Address != orderDto.Address)
                msg = "[地址信息]";
            else if (old.VehicleInfos != orderDto.VehicleInfos)
                msg = "[车辆信息]";
            var order = orderDto.ToEntity(old);
            OrderBLL.SetOrderChangeRecord(order, DiscountType.BaseInfo, ClientHelper.ClientInfo, 0, null, msg);
            directSaleSvc.Edit(order);
            return true;
        }

        /// <summary>
        /// 编辑商品，如果商品重量为0，则从订单中删除
        /// </summary>
        /// <param name="ord">计划单实体</param>
        /// <returns></returns>
        [Route("CommodityInfoEdit")]
        public bool CommodityInfoEdit(Order ord)//(CommodityInfoDto[] cmmdInfoDtos1)
        {
            //Order ord = null;
            var cmmdInfoDtos = ord.CommodityInfos;
            using (var tran = new TransactionScope())
            {
                var ciBll = BllFactory.GetBll<BaseBLL<CommodityInfo, Guid>>();
                var commBll = BllFactory.GetBll<BaseBLL<Commodity, Guid>>();
                var order = orderBll.Details(ord.Id);
                var oldTotal = order.Total;
                directSaleSvc.CommodityInfoEdit(ord, ClientHelper.ClientInfo);
                order.CalcNew();
                directSaleSvc.Edit(order);
                if (order.IsSettlement)
                {
                    directSaleSvc.Settlement(ClientHelper.ClientInfo.Id, order.Id);
                }
                tran.Complete();
            }
            return true;
        }

        /// <summary>
        /// 结算计划单
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        /// <returns></returns>
        [Route("OrderSettlement")]
        public IResultState OrderSettlement(Guid orderId)
        {
            var old = orderBll.Details(orderId);
            if (old.State != OrderState.Send)
            {
                return ReState.ArgError.Create("订单状态错误");
            }
            return directSaleSvc.Settlement(ClientHelper.ClientInfo.Id, orderId);

            //OrderBLL.SetOrderChangeRecord(old, DiscountType.Msg, ClientHelper.ClientInfo, old.Total, "结算计划单");
            //orderBll.EditAndSettlement(old);

        }

        /// <summary>
        /// 运营发货
        /// </summary>
        /// <param name="ord">计划单实体</param>
        /// <returns></returns>
        [Route("EditSendInfo")]
        public IResultState PutEditSendInfo(Order ord)
        {
            var order = orderBll.Details(ord.Id);
            if (order == null || order.State != OrderState.Checked)
            {
                return ReState.ArgError.Create("错误的订单信息");
            }
            if (order.Provider.OperationsMan.Id != ClientHelper.ClientInfo.Id)
            {
                return ReState.ArgError.Create("您不是该供应商对应运营人员");
            }
            var empId = ClientHelper.ClientInfo.Id;
            var state = directSaleSvc.OrderSend(empId, ord.Id);
            if (state.State == (int)ReState.Success)
            {
                var str = string.Format("{0:M/d HH:mm} 运营人员[{1}]已经成功发货", DateTime.Now, order.Provider.OperationsMan.Name);
                var nb = new NoticeInfoBLL();
                nb.SendMsg(str, order.Creater.Id, null);
            }
            using (var tran = new TransactionScope())
            {
                directSaleSvc.RealWeight(ord, ClientHelper.ClientInfo);
                directSaleSvc.ToDepotDelivery(ord.Id, ClientHelper.ClientInfo.Id);
                tran.Complete();
            }
            return ReState.Success.Create();
        }

        #endregion

    }
}
